<template>
  <el-dialog :title="isEdit ? '修改单位信息': '新增单位信息'" :visible.sync="visible" :close-on-click-modal="closeOnClickModal" @open="_handleDialogOpen" @close="_cancel">
    <el-form ref="dataForm" :model="form" :status-icon="true" :rules="formRules" label-width="100px">
      <el-form-item v-show="!!parentData" label="上级单位">
        <el-link>{{ parentData ? parentData.name : '' }}</el-link>
      </el-form-item>
      <el-form-item label="单位名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入名称"/>
      </el-form-item>
      <el-form-item label="单位别名" prop="aliasName">
        <el-input v-model="form.aliasName" placeholder="请输入别名"/>
      </el-form-item>
      <el-form-item label="单位编码" prop="code">
        <el-input v-model="form.code" placeholder="请输入编码"/>
      </el-form-item>
      <el-form-item label="单位编码" prop="code">
        <el-input v-model="form.code" placeholder="请输入编码"/>
      </el-form-item>
      <el-form-item label="联系人一姓名" prop="contact1">
        <el-input v-model="form.contact1" placeholder="请输入联系人一姓名"/>
      </el-form-item>
      <el-form-item label="联系人一电话" prop="mobile1">
        <el-input v-model="form.mobile1" placeholder="请输入联系人一电话"/>
      </el-form-item>
      <el-form-item label="联系人二姓名" prop="contact2">
        <el-input v-model="form.contact2" placeholder="请输入联系人二姓名"/>
      </el-form-item>
      <el-form-item label="联系人二电话" prop="mobile2">
        <el-input v-model="form.mobile2" placeholder="请输入联系人二电话"/>
      </el-form-item>
      <el-form-item label="联系人三姓名" prop="contact3">
        <el-input v-model="form.contact3" placeholder="请输入联系人三姓名"/>
      </el-form-item>
      <el-form-item label="联系人三电话" prop="mobile3">
        <el-input v-model="form.mobile3" placeholder="请输入联系人三电话"/>
      </el-form-item>
      <el-form-item label="排序号" prop="sortNo">
        <el-input-number v-model="form.sortNo" placeholder="请输入排序号"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="_cancel">取 消</el-button>
      <el-button :loading="_loading" size="small" type="primary" @click="_ok">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>

import organApi from '@/api/systemManage/organ'

export default {
  props: {
    closeOnClickModal: {
      type: Boolean,
      default: false
    },

    loading: {
      type: Boolean,
      default: false
    }
  },

  data() {
    return {
      isEdit: false,
      visible: false,
      parentData: null,
      form: {},
      cityList: [],
      formRules: {
        name: [{
          required: true,
          message: '请输入名称'
        }],
        code: [{
          required: true,
          message: '请输入编码'
        }],
        typeCode: [{
          required: true,
          message: '请选择分类'
        }]
      }
    }
  },

  computed: {
    '_loading': {
      get() {
        return this.loading
      },

      set(value) {
        this.$emit('update:loading', value)
      }
    }
  },

  methods: {
    init() {
    },

    _formReset() {
      this.form = {
        name: '',
        aliasName: '',
        code: '',
        typeCode: '',
        tag: '',
        address: '',
        contact1: '',
        mobile1: '',
        contact2: '',
        mobile2: '',
        contact3: '',
        mobile3: '',
        sortNo: 1
      }
      this.$nextTick(() => {
        this.$refs.dataForm.resetFields()
      })
    },

    _handleDialogOpen() {
      this.init()
      this._formReset()
      this.$emit('open')
    },

    _cancel() {
      this._formReset()
      this.visible = false
      this.isLoading = false
      this.$emit('cancel')
    },

    _ok() {
      const form = this.$refs.dataForm
      form.validate(valid => {
        if (valid) {
          const formData = JSON.parse(JSON.stringify(this.form))
          this.$emit('ok', formData, this.isEdit)
        }
      })
    },

    show(params, parent) {
      this.visible = true
      this.parentData = parent
      if (params) {
        this.isEdit = true
        organApi.getById({
          id: params.id
        }).then(resp => {
          this.form = resp.data
        }).catch(resp => {
          this.$message.error(resp.message)
        })
      } else {
        // pass
        this.isEdit = false
        if (parent && parent.data) {
          setTimeout(() => {
            this.form.typeCode = parent.data.typeCode
            this.form.tag = parent.data.tag
          }, 0)
        }
        // this._clear()
      }
    },

    close() {
      this._cancel()
    }
  }
}
</script>
